const db = require('./db.js')
const moment = require('moment');
const pyfl = require('pyfl').default

module.exports = {
	// 获取好友列表
	UserFriendList:async ({user_id,no_group})=>{
		const sql = `SELECT a.user_nickname,a.user_id,a.user_icon,b.remake,b.letter FROM user a LEFT JOIN user_relation b on b.friend_id = a.user_id where b.user_id = ${user_id} ORDER BY letter ASC`
		const list = await db.query(sql)
		// 不分组返回
		if(no_group){
			return list
		}
		const newList =  []
		list.forEach((item,index)=>{
			let indexLetter = newList.findIndex(ul=>{return ul.letter == item.letter})
			if(indexLetter!==-1){
				newList[indexLetter].data.push(item)
			}else{
				newList.push({
					letter:item.letter,
					data:[item]
				})
			}
		})
		return newList
	},
	// 添加好友
	UserFriendAdd:async ({user_id,friend_id,user_nickname,friend_nickname}) => {
		const sql = `insert into user_relation(user_id,friend_id,relation_time,letter) values('${user_id}','${friend_id}','${moment().format('YYYY-MM-DD HH:mm:ss')}','${pyfl(friend_nickname)[0]}'),('${friend_id}','${user_id}','${moment().format('YYYY-MM-DD HH:mm:ss')}','${pyfl(user_nickname)[0]}')`
		return await db.query(sql)
	},
	// 存入离线好友申请
	UserFriendAddMsg:async ({from_user_id,to_user_id,remake,to_user_icon,from_user_icon,from_user_sex,from_user_nickname,is_apply})=>{
		const sql =`insert into friends_apply(from_user_id,to_user_id,remake,to_user_icon,from_user_icon,from_user_sex,from_user_nickname,is_apply) values(${from_user_id},${to_user_id},'${remake}','${to_user_icon}','${from_user_icon}','${from_user_sex}','${from_user_nickname}','${is_apply}')`
		return await db.query(sql)
	},
	// 获取离线好友申请
	UserFriendAddOffLineMsg:async (user_id)=>{
		const sql = `select * from friends_apply where to_user_id = ${user_id}`
		return await db.query(sql)
	},
	// 删除离线好友申请
	UserFriendAddOffLineDelete:async (user_id)=>{
		const sql = `DELETE FROM friends_apply WHERE to_user_id = ${user_id}`
		return await db.query(sql)
	},
	// 存入离线消息
	UserSaveOffLineMsg:async (data) => {
		const sql = `insert into user_message_off_line(content,message_type,type,to_user,from_user,send_time) values('${JSON.stringify(data.content)}','${data.message_type}','${data.type}',${data.to_user},${data.from_user},'${data.send_time}')`
		return await db.query(sql)
	},
	// 获取用户离线消息
	UserOffLineMsg:async (user_id) =>{
		const sql = `select * from user_message_off_line where to_user = ${user_id}`
		return await db.query(sql)
	},
	// 删除离线消息
	UserDeleteOffLineMsg:async (user_id)=>{
		const sql = `DELETE FROM user_message_off_line WHERE to_user = ${user_id}`
		return await db.query(sql)
	},
	// 保存消息
	UserSaveMsg:async (data)=>{
		const sql = `insert into user_message(content,message_type,type,to_user,from_user,send_time) values('${JSON.stringify(data.content)}','${data.message_type}','${data.type}',${data.to_user},${data.from_user},'${data.send_time}')`
		return await db.query(sql)
	},
	// 获取用户聊天id
	getUserSocketId:async(user_id)=>{
		const sql = `select user_socketid from user where user_id = ${user_id}`
		return await db.query(sql)
	}
}